 <template>
    <div class="index-page">
        <!-- 顶部按钮 -->
        <el-button type="info" class="btn" :loading="searchLoading" @click="search" icon="search">搜索</el-button>
        <el-button type="success" class="btn" @click='componentsShow("add")' icon="edit">添加</el-button>
        <!-- <el-button type="danger" class="btn" :disabled="multipleDeleteDisabled" icon="delete" @click="componentsShow('multipleDelete','')">批量删除</el-button> -->

        <!-- 模糊搜索条件 -->
        <div class="search">

            <div class="search-item">
                <span>工号名称：</span>
                <el-input placeholder="请输入班级名称" class="code-search" v-model="keyword" icon="search" @keyup.enter.native="search"></el-input>
            </div>

        </div>

        <!-- 数据表格展示区域 -->
        <el-table
            :data="dataList"
            border
            align="center"
            class="admin-table"
            @select="checkSelect"
            @select-all="checkall">
            <div slot='empty'>
                <img src="../../../assets/img/empty.png" style="width:40px;vertical-align: middle;">
                <span style="vertical-align: middle;">暂时还没有内容哦!</span>
            </div>

            <el-table-column type="selection" width="55"></el-table-column>

            <el-table-column
                :prop="v.prop"
                :label="v.label"
                v-for="(v,index) in portTitleList"
                align="center"
                show-overflow-tooltip
                v-if="v.hide != 0"
                :align="v.align || 'center'"
                :fixed="v.fixed"
                :formatter="v.formatter"
                :class-name="v.columnStyle"
                :width="v.width"
                :min-width="v.minWidth || '150'"
                :header-align="v.headerAlign || 'center'"
                >
                <template scope="scope">
                    <div slot="reference" v-if="scope.column.property == 'mc'">
                        <span class="classInfo" @click="componentsShow('info',scope)">{{ scope.row[scope.column.property] }}</span>
                    </div>
                    <div slot="reference" v-else-if="scope.column.property == 'sj'" v-html="$.getLocalTime(scope.row[scope.column.property])"></div>
                    <div slot="reference" v-else>
                        <span>{{ scope.row[scope.column.property] }}</span>
                    </div>
                </template>
            </el-table-column>

            <el-table-column
                label="操作"
                width="250"
                align="center"
                fixed="right">
                <template scope="scope">
                    <el-button size="small" type="text" @click="componentsShow(v.type,scope)" v-for="v in portBntList" v-if="v.name !== '禁用' && v.name !== '密码重置'">{{v.name}}</el-button>
                    <el-button size="small" type="text" @click="isqy(scope)" v-else-if="v.name === '禁用'">{{ scope.row.isqy == 'Y' ? '启用' : '禁用' }}</el-button>
                    <el-button size="small" type="text" @click="changePwd(scope)" v-else>{{v.name}}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页查询区域 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="pageindex"
            :page-sizes="pagesizes"
            :page-size="pagesize"
            :layout="layout"
            :total="total"
            class="class-page">
        </el-pagination>


        <!-- 各个组件显示区域 -->

        <!-- 添加组件 -->
        <AddComponent v-if="portComponents.add.toggle"  @close="componentsHide"  :params="portComponents.add.params"></AddComponent>

        <!-- 查看班级组件 -->
        <!-- <ClassComponent v-if="portComponents.class.toggle"  @close="componentsHide"  :params="portComponents.class.params"></ClassComponent> -->

        <!-- 信息组件 -->
        <InfoComponent v-if="portComponents.info.toggle"  @close="componentsHide"  :params="portComponents.info.params"></InfoComponent>

        <!-- 权限组件 -->
        <AuthComponent v-if="portComponents.authority.toggle"  @close="componentsHide"  :params="portComponents.authority.params"></AuthComponent>

        <!-- 编辑组件 -->
        <EditComponent v-if="portComponents.edit.toggle"  @close="componentsHide"  :params="portComponents.edit.params"></EditComponent>

        <!-- 子路由显示区域 -->
        <router-view class="port-view"></router-view>
    </div>
</template>

<script>
    import Vue from 'vue'
    import AddComponent from './add'
    import InfoComponent from './info'
    import EditComponent from './edit'
    import AuthComponent from './authority'
    // import ClassComponent from './class'

    export default {
        components: {
            // LoadingComponent,
            // DeleteComponent,
            AddComponent,
            // ClassComponent,
            EditComponent,
            InfoComponent,
            AuthComponent
            // UpgradeComponent,
            // ChangeComponent,
            // TestComponent
        },
        data () {
            return {
                keyword: '',
                /*组件区域*/
                portComponents: {
                    singleDelete: {
                        type: 'singleDelete',
                        toggle: false,
                        params: '',
                        data: {
                            index: '',
                            type: 'single',
                            title: '此操作将删除该班级, 是否继续?'
                        }
                    },
                    multipleDelete: {
                        type: 'multipleDelete',
                        toggle: false,
                        params: '',
                        data: {
                            index: '',
                            type: 'multiple',
                            title: '此操作将删除您选中的班级, 是否继续?'
                        }
                    },
                    add: {
                        type: 'add',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    info: {
                        type: 'info',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    edit: {
                        type: 'edit',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    class: {
                        type: 'class',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                     forbidden: {
                        type: ' forbidden',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    authority: {
                        type: 'authority',
                        toggle: false,
                        params: '',
                        data: {}
                    },
                    reset: {
                        type: 'reset',
                        toggle: false,
                        params: '',
                        data: {}
                    }
                },

                /*
                    prop :  接口参数名
                    label : 接口中文名
                    hide : 是否隐藏这一列
                    align : 这一列对齐顺序 ： left,right,center
                    headerAlign : 这一列表头对齐方式 ： left,right,center
                    formatter: function (row, column) {}
                */
                portTitleList: [
                    {
                        prop:'id',
                        label:'ID',
                        hide:0,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'gh',
                        label:'工号',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'zwm',
                        label:'老师中文名',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'ywm',
                        label:'老师英文名',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'emptype',
                        label:'类型',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'dh',
                        label:'电话',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'xxmc',
                        label:'学校',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'bjs',
                        label:'班级数',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    },
                    {
                        prop:'sfqy',
                        label:'是否启用',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center'
                    }

                ],
                portBntList: [//接口按钮列表
                    // {
                    //     name:'详情',
                    //     type:'info'
                    // },
                    {
                        name:'修改',
                        type:'edit'
                    },
                    // {
                    //     name:'查看班级',
                    //     type:'class'
                    // },
                    {
                        name:'禁用',
                        type:'forbidden'
                    },
                    {
                        name:'密码重置',
                        type:'reset'
                    },
                    {
                        name:'工号权限',
                        type:'authority'
                    },
                    // {
                    //     name:'删除',
                    //     type:'singleDelete'
                    // }
                ],

                multipleDelNum:'', //获取批量选择的数据
                dataList: [],  //表格数据

                /*动画加载*/
                isLoading: false,//动画加载
                searchLoading: false,//搜索按钮触发按钮动画

                /*模糊搜索条件*/
                inputbjmc:'' , //  班级名称,


                /*分页属性设置*/
                layout: 'total, sizes, prev, pager, next, jumper',//组件布局
                pageindex: 1,//当前分页页码
                pagesizes: [12, 24, 36, 72],//显示个数的选择器选项
                pagesize: 12,//每页显示条目数
                total: 0,//总条数


                dropDownData:'' //
            }
        },
        created () {

            //加载页面时调用
            this.$store.dispatch('changeTitle','工号管理');//修改title
            this.$store.dispatch('changeRouterIndex', '/admin/numberManage');//修改路由

            this.search();//分页获取默认数据
            // this.getdropDownData();//获取下拉框数据

        },
        methods: {

            search () {//查询搜索触发事件
                this.searchLoading = true;
                this.axios.get( this.$store.state.api + 'ls/list',{
                    params:{
                        gjz:this.keyword,
                        pageindex: this.pageindex,
                        pagesize: this.pagesize
                    }
                }).then( res => {
                    console.log(res)

                    let data = res.data.data;
                    let list = data.list;
                    this.total = data.totalPage;
                    this.dataList = list;

                    this.searchLoading = false;
                }).catch( res => {
                    console.log(res)
                    this.searchLoading = false;
                })
            },

            isqy (scope) {
                var isqy = scope.row.isqy === 'Y' ? 'N' : 'Y';
                this.$store.state.loading = true;
                this.axios.get( this.$store.state.api + 'ls/qy',{
                    params:{
                        id: scope.row.id,
                        isqy: isqy
                    }
                }).then( res => {
                    console.log(res)
                    if (res.data.code == 1) {
                        this.search()
                    }
                    this.$store.state.loading = false;
                }).catch( res => {
                    console.log(res)
                    this.$store.state.loading = false;
                })
            },
            changePwd (scope) {
                this.$store.state.loading = true;
                this.axios.get( this.$store.state.api + 'ls/mm',{
                    params:{
                        id: scope.row.id
                    }
                }).then( res => {
                    console.log(res)
                    if (res.data.code == 1) {
                        this.search()
                    }
                    this.$store.state.loading = false;
                }).catch( res => {
                    console.log(res)
                    this.$store.state.loading = false;
                })
            },



            handleSizeChange (val) {//每页显示条数触发事件
                this.pagesize = val;
                this.pageindex = 1;
                this.search();
            },

            handleCurrentChange (val) {//分页触发事件
                this.search();
            },
            checkSelect (selection, row) {//点击单选select触发事件
                console.log(selection, row)
                let strID = [];
                selection.forEach(function(v,i){
                    strID.push(v.id)
                })
                this.multipleDelNum = strID.join(",");
            },
            checkall (selection) {//点击全选select触发事件
                // console.log(selection)
                let strID = [];

                selection.forEach(function(v,i){
                    strID.push(v.id)
                })
                // console.log(strID.join(","))
                this.multipleDelNum = strID.join(",");
                // console.log(strID)
            },







            /* 组件的弹框  */
            componentsShow (type,data) { //显示组件
                console.log(type,data)
                for ( let i in this.portComponents ) {
                    if (type == this.portComponents[i].type) {

                        this.portComponents[i].toggle = true;
                        this.portComponents[i].params = data;

                        if (this.portComponents[i].data.type == 'single') {
                            this.portComponents[i].data.index = data.row.id

                        }else if(this.portComponents[i].data.type == 'multiple'){
                            this.portComponents[i].data.index = this.multipleDelNum;

                        }else if(this.portComponents[i].type == 'test'){
                            this.portComponents[i].data.distinction  = "test";

                        }else if(this.portComponents[i].type == 'score'){
                            this.portComponents[i].data.distinction  = "score";

                        }else{
                            return;
                        }
                        return;
                    }
                }

            },
            componentsHide (setting) { //关闭组件
                console.log(setting)
                for ( let i in this.portComponents ) {
                    if (setting.type == this.portComponents[i].type) {
                        this.portComponents[i].toggle = false;

                        if (!setting.cancel) {
                           this.search();
                        }
                        return;
                    }
                }
            }
        },
        computed: {
            multipleDeleteDisabled () {
                console.log(this.multipleDelNum)
                if (this.multipleDelNum) {
                    return false;
                }else{
                    return true
                }
            }
        }
    }
</script>

<style lang="less" scoped>
    .index-page {
        font-size:12px;
        position: relative;

        .btn {
            height: 30px;
            padding:0 10px;
        }

        .classInfo:hover {
            color:#4094c7;
            cursor:pointer;
            text-decoration: underline;
        }

        .code-search {
            width: 200px;
            height: 30px;
            margin:10px 0 10px 0;
        }

        .change-time{
            width: 480px;
            display: inline-block;
        }

        .class-page {
            text-align: left;
            margin-top:10px;
        }

        .search-item {
            display: inline-block;
            margin-right:10px;
        }

        h2{
            line-height: 40px;
        }

    }
</style>
